
package lion.framework.core.web.views;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;

import lion.framework.core.util.AppPaths;
import lion.framework.core.web.anno.ViewType;
import lion.framework.core.web.path.ParameterizedPath;
import lion.framework.core.web.request.RequestModel;

/**
 * 该视图用于重定向，可以使用参数化的路径，如/a/{theme}/b?id={id}，参数的内容通过model.addModel进行设置 。
 *
 * <pre>
 * time   2013 2013-5-14 下午04:53:52
 * mail   hl_0074@sina.com
 * </pre>
 *
 * @author lion
 */
@ViewType("redirect")
public class RedirectView implements IView {

	public static final String VIEW_TYPE = "redirect";
	private String name;
	private String path;

	public RedirectView() {

	}

	public RedirectView(String path) {

		this.path = path;
	}

	@Override
	public void render(HttpServletRequest request, HttpServletResponse response) {

		String path = ParameterizedPath.mapPath(this.path, new RequestModel(request));

		if (path.startsWith("/")) {
			path = FilenameUtils.separatorsToUnix(AppPaths.WEBCONTEXT + path);
		}
		try {
			response.sendRedirect(path);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public String getName() {

		return this.name;
	}

	@Override
	public String type() {

		return VIEW_TYPE;
	}

}
